﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyKho
{
    public partial class uctTienTe : UserControl
    {
        private TaiKhoan taiKhoan;
        public uctTienTe(TaiKhoan _taiKhoan)
        {
            InitializeComponent();
            taiKhoan = _taiKhoan;
        }

        private void gctTienTe_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (gvTienTe.GetDataRow(gvTienTe.GetSelectedRows()[0])[gclKyHieu.FieldName].ToString() == "VND")
                        mXoa_mGctTienTe.Enabled = false;
                    else
                        mXoa_mGctTienTe.Enabled = true;
                    mGctTienTe.Show(gctTienTe, new System.Drawing.Point(e.X, e.Y));
                }
            }
            catch { }
        }

        private void gvTienTe_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                TienTe tienTe = new TienTe();
                tienTe.KiHieu = gvTienTe.GetDataRow(e.RowHandle)[gclKyHieu.FieldName].ToString();
                tienTe.LoaiTien = gvTienTe.GetDataRow(e.RowHandle)[gclLoaiTien.FieldName].ToString();
                tienTe.TyGia = Convert.ToDouble(gvTienTe.GetDataRow(e.RowHandle)[gclTyGia.FieldName].ToString());

                if (gvTienTe.IsNewItemRow(e.RowHandle))
                    tienTe.insert(taiKhoan);
                else
                    tienTe.update(taiKhoan);

            }
            catch { }
        }

        private void gvTienTe_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            int[] x = gvTienTe.GetSelectedRows();
            if (!gvTienTe.IsNewItemRow(x[0]))
                if (gvTienTe.GetDataRow(x[0])[gclKyHieu.FieldName].ToString() == "VND" || gvTienTe.FocusedColumn == gclKyHieu)
                    e.Cancel = true;
        }

        private void mThem_mGctTienTe_Click(object sender, EventArgs e)
        {
            if (mThem_mGctTienTe.Checked)
            {
                mThem_mGctTienTe.Checked = false;
                gvTienTe.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            }
            else
            {
                mThem_mGctTienTe.Checked = true;
                gvTienTe.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            }
        }

        private void mXoa_mGctTienTe_Click(object sender, EventArgs e)
        {
            try
            {
                TienTe tienTe = new TienTe();
                tienTe.KiHieu = gvTienTe.GetDataRow(gvTienTe.GetSelectedRows()[0])[gclKyHieu.FieldName].ToString();

                if (MessageBox.Show("Bạn có muốn xóa Đơn vị tiền tệ \"" + gvTienTe.GetDataRow(gvTienTe.GetSelectedRows()[0])[gclLoaiTien.FieldName].ToString() + "\" không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    tienTe.delete(taiKhoan);
                    gvTienTe.DeleteRow(gvTienTe.GetSelectedRows()[0]);
                }
            }
            catch { }
        }

        private void uctTienTe_Load(object sender, EventArgs e)
        {
            gctTienTe.DataSource = TienTe.getList(taiKhoan);
            gclKyHieu.FieldName = "KyHieu";
            gclLoaiTien.FieldName = "LoaiTien";
            gclTyGia.FieldName = "TyGia";
        }


    }
}
